package cn.common;

import cn.kpi.common.Excel.util.ReflectionUtils;
import cn.kpi.entity.payassessment.AssessRulePay;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 绩效考核规则
 *
 * @author lex
 * @create 2018-07-05-22:20
 */
public class PayMoneyRule {

    public static void getPayAmount(Object objectPayVoEntity, Map<String,List<String>> ruleMap) throws Exception {
        Float originMoney = Float.valueOf(ruleMap.get("fullMoney").get(0));
        for (Field field : objectPayVoEntity.getClass().getDeclaredFields()){
            String MethedName = field.getName();
            if(ruleMap.containsKey(MethedName)){
                List<String> list = ruleMap.get(MethedName);
                Float nowMoney = Float.valueOf(ReflectionUtils.executeMethod(objectPayVoEntity,ReflectionUtils.returnGetMethodName("nowMoney")).toString());
                Float day = Float.valueOf(ReflectionUtils.executeMethod(objectPayVoEntity,ReflectionUtils.returnGetMethodName(MethedName)).toString());
                nowMoney = getCheckRule(list,originMoney,nowMoney,day);
                ReflectionUtils.executeSetMethod(objectPayVoEntity,ReflectionUtils.returnSetMethodName("nowMoney"),nowMoney);//回写计算完的钱数
            }
        }
    }


    public static   Float getCheckRule(List<String> rules,Float orgnMoney, Float nowMoney, Float day){
        String type = rules.get(0);
        String sum =rules.get(1);
        if(sum != null && sum.equals("")&& nowMoney >0f) {
            switch (type) {
                case "1": nowMoney = nowMoney - Float.valueOf(sum) * day;
                    break;
                case "2": if(day > Float.valueOf(sum)) nowMoney = nowMoney - 1/3*orgnMoney;
                    break;
                case "3": if(day > Float.valueOf(sum)) nowMoney = 0f;
                    break;
            }
        }
        if(nowMoney > 0f)
            return nowMoney;
        else
            return 0f;
    }

    public static Map<String,List<String>> toMapRule(AssessRulePay assessRulePay) throws Exception {
        Map<String,List<String>> retMap = new HashMap<>();
        for (Field field : assessRulePay.getClass().getDeclaredFields()){
            String MethedName = field.getName();
            if(!MethedName.equals("LAY_TABLE_INDEX")) {
                if (!MethedName.contains("module") && !MethedName.contains("Id") && !MethedName.contains("Sum") && !MethedName.contains("Type") &&
                        !MethedName.contains("TABLE") && !MethedName.contains("full")) {
                    String type = ReflectionUtils.executeMethod(assessRulePay, ReflectionUtils.returnGetMethodName(MethedName) + "Type").toString();
                    String sum = ReflectionUtils.executeMethod(assessRulePay, ReflectionUtils.returnGetMethodName(MethedName) + "Sum").toString();
                    List<String> list = new ArrayList<>();
                    list.add(type);
                    list.add(sum);
                    retMap.put(MethedName, list);
                } else {
                    String value = ReflectionUtils.executeMethod(assessRulePay, ReflectionUtils.returnGetMethodName(MethedName)).toString();
                    List<String> list = new ArrayList<>();
                    list.add(value);
                    retMap.put(MethedName, list);

                }
            }
        }
        return  retMap;
    }
}
